package com.malou.client.ventas.separacion;

import java.util.Date;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.Validator;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.NumberFormat;
import com.malou.client.MalouConstants;

public class PagarSeparacionDialog extends Dialog {
	public HiddenField<String> separacion_id;
	public NumberField monto;
	public HiddenField<String> montoh;
	private Button guardar;
	private Button cancelar;
	public Status status;

	PagarSeparacion pagsep;
	private MalouConstants constants = GWT.create(MalouConstants.class);
	
	public PagarSeparacionDialog(){
		setLayout(new FitLayout());
		setModal(true);
		setPlain(true);
		setSize(300, 120);
		setClosable(true);
		
		FormPanel panel = new FormPanel();		
		panel.setBorders(false);
	    panel.setBodyBorder(false);
	    panel.setPadding(5);
	    panel.setHeaderVisible(false);
	    panel.setLabelWidth(110);
	        
	    addFields(panel);
	    
	    setButtonAlign(HorizontalAlignment.LEFT);
	    setButtons("");
	    add(panel);
	    
	    setBodyBorder(true);	    
	    setResizable(false);	    
	    setFocusWidget(monto);
	    
	    setIconStyle("icon-pagar");
		setHeading(constants.separacionPagar());
	}
	
	@Override
	  protected void createButtons() {
	    super.createButtons();
	    
	    if (constants == null) {
	    	constants = GWT.create(MalouConstants.class);
		}
	    
	    status = new Status();
	    status.setBusy(constants.guardando());
	    status.hide();
	    status.setAutoWidth(true);
	    getButtonBar().add(status);
	    
	    getButtonBar().add(new FillToolItem());
	    
	    cancelar = new Button(constants.cancelar());
	    
	    cancelar.addSelectionListener(new SelectionListener<ButtonEvent>() {
	      public void componentSelected(ButtonEvent ce) {
	    	  PagarSeparacionDialog.this.hide();
	      }

	    });

	    guardar = new Button(constants.guardar());
	    
	    guardar.addSelectionListener(new SelectionListener<ButtonEvent>() {
	      public void componentSelected(ButtonEvent ce) {	    	  
	    	  if(validate()){
	    		status.show();
		    getButtonBar().disable();
		    
		    pagsep = new PagarSeparacion();			
		    pagsep.setSeparacion_id(Integer.valueOf(separacion_id.getValue().toString()));		    
		    pagsep.setFecha(new Date());		    
		    pagsep.setHora(new Date());
		    pagsep.setMonto(Double.parseDouble(monto.getValue().toString()));
		    
			Separaciones.pagarSeparacionDialog = PagarSeparacionDialog.this;   			
			Separaciones.pagar(pagsep);	    			
	    	}	    		 
	    	  
	      }
	    });
	    
	    addButton(guardar);
	    addButton(cancelar);	    
	    
	}
	
	private boolean validate() {
		boolean mn = monto.isValid();				
	    return mn;
	}
	
	public void reset(){	  
		  		  
		  separacion_id.clear();		  
		  monto.clear();
		  montoh.clear();
		  
		  status.hide();
		  getButtonBar().enable();
	}
	
	private void addFields(ContentPanel cp) {

		separacion_id = new HiddenField<String>();
		separacion_id.setVisible(false);
		cp.add(separacion_id);
		
		montoh = new HiddenField<String>();
		montoh.setVisible(false);
		cp.add(montoh);		
	    	    
	    monto = new NumberField();
	    monto.setFieldLabel(constants.separacionMonto());
	    monto.setAllowNegative(false);
	    monto.setAllowBlank(false);
	    monto.setAllowDecimals(true);
	    monto.setPropertyEditorType(Double.class);
	    monto.setFormat(NumberFormat.getFormat("0.00"));
	    
	    final String errMsg = constants.separacionMontoMenorSaldo();
	    monto.setValidator(new Validator() {
	      public String validate(Field<?> field, String value) {
	    	  if(montoh.getValue() != null)
	    	  if(Double.parseDouble(value) > Double.parseDouble(montoh.getValue()))	        
	          return errMsg;
	        return null;
	      }
	    });
	    cp.add(monto, new FormData("-20"));		    

   }
}
